package thread.threadpoolpackage;


import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 创建线程池
 * Created by cuihao on 2017/9/6.
 */
public class ExecutorUtil {
    public static ExecutorService ThreadPool() {
        /*固定*/
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
        ExecutorService customFixedThreadPool = new ThreadPoolExecutor(10, 10, 0,
            TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>());

        /*单个*/
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        ExecutorService customSingleThreadExecutor = new ThreadPoolExecutor(1, 1, 0,
            TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>());

        /*可缓存*/
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        ExecutorService customCacheThreadPool = new ThreadPoolExecutor(0, 500, 1000L,
            TimeUnit.MILLISECONDS, new SynchronousQueue<>());


        /*核心数等于最大线程数，不会拒绝任务，有任务就排在队列里，用于负载重的服务器上*/
//        return fixedThreadPool;

        /*只有一个核心线程，任务队列是链表，所以按照顺序执行(打印结果会是从0-999)，不会拒绝任务*/
        //return singleThreadExecutor;

        /*没有核心线程，可以有多个外包线程，空闲时间已过就销毁，没有空闲线程就一直创建，用于负载轻的服务器上*/
        return cachedThreadPool;
    }

    public static void main(String[] args) {

        ExecutorService threadPool = ThreadPool();

        /* 调用方法1：使用execute方法*/
        for (int i = 0; i < 3000; i++) {
            int s = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(s);
                    System.out.println(Thread.currentThread().getThreadGroup() + "--" + Thread.currentThread().getId() +
                        "--" + Thread.currentThread().getName());
                }
            });
        }

        /*调用方法2：使用submit方法返回结果为Future*/
        for (int i = 0; i < 3000; i++) {
            int s = i;
            Future<?> future = threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(s);
                    System.out.println(Thread.currentThread().getThreadGroup() + "--" + Thread.currentThread().getId() +
                        "--" + Thread.currentThread().getName());
                }
            });
            try {
                Object o = future.get();
                System.out.println(o + "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        try {
            //不用singlepool时 不想打印顺序错乱
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.currentTimeMillis();

        /* 线程终止方法*/
        //1，shutdown只是将线程池的状态设置为SHUTWDOWN状态，正在执行的任务会继续执行下去，没有被执行的则中断。
        //threadPool.shutdown();
        //2，shutdownNow则是将线程池的状态设置为STOP，正在执行的任务则被停止，没被执行任务的则返回。
        List<Runnable> undoList = threadPool.shutdownNow();
        System.out.println("isShutDown: " + threadPool.isShutdown());
        System.out.println("isTerminated: " + threadPool.isTerminated());
        System.out.println("线程池已关闭");
    }
}